package com.lottery.ruok.lottrey.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.fragment.LXCNumberTrendFragment;
import com.lottery.ruok.lottrey.fragment.LXCTrendFragment;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetCurrentIssueNoRequest;
import com.lottery.ruok.lottrey.retrofitModule.request.LiuhecaiTrendRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetCurrentIssueNoResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.LiuhecaiTrendResponse;
import com.lottery.ruok.lottrey.ui.ShapeIndicatorView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import utils.Utils;

/*
* 号码走势
* */

public class LhcChartActivity extends BaseActivity{

    @BindView(R.id.custom_indicator)
    ShapeIndicatorView shapeIndicatorView;
    @BindView(R.id.tabs)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private CountDownTimer countDownTimer;
    @BindView(R.id.time)
    TextView time;

    private ArrayList<LiuhecaiTrendResponse> liuhecaiTrendResponseList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lxc_chart);
        ButterKnife.bind(this);

        init();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    private void setTimer() {
        RetrofitBase.getRetrofit().create(GetCurrentIssueNoRequest.class).getResult(getIntent().getStringExtra("code"))
                .compose(RxHelper.<GetCurrentIssueNoResponse>handleResultReConnection())
                .subscribe(new RxSubscribe<GetCurrentIssueNoResponse>(this) {
                    @Override
                    protected void _onNext(final GetCurrentIssueNoResponse getCurrentIssueNoResponse) {
                        if(countDownTimer != null){
                            countDownTimer.cancel();
                        }
                        //启动定时器
                        countDownTimer = new CountDownTimer(getCurrentIssueNoResponse.getTime() * 1000, 1000) {
                            @Override
                            public void onTick(long l) {
                                time.setText(Html.fromHtml("<font color='#989898'>开奖倒计时</font>" + Utils.getCountTimeByLong(l)));
                            }

                            @Override
                            public void onFinish() {
                                time.setText(Html.fromHtml("<font color='#989898'>开奖倒计时</font>" + "00:00:00"));

                                time.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        setTimer();
                                    }
                                }, 2000);
                            }
                        };
                        countDownTimer.start();
                    }

                    @Override
                    protected boolean showDialog() {
                        return false;
                    }
                });
    }

    private void init() {
        setTimer();

        RetrofitBase.getRetrofit().create(LiuhecaiTrendRequest.class).getResult(getIntent().getStringExtra("code"))
                .compose(RxHelper.<ArrayList<LiuhecaiTrendResponse>>handleResult())
                .subscribe(new RxSubscribe<ArrayList<LiuhecaiTrendResponse>>(this) {
                    @Override
                    protected void _onNext(ArrayList<LiuhecaiTrendResponse> liuhecaiTrendResponse) {
                        liuhecaiTrendResponseList = liuhecaiTrendResponse;

                        viewPager.setOffscreenPageLimit(3);

                        viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager()));

                        mTabLayout.setupWithViewPager(viewPager);

                        shapeIndicatorView.setupWithTabLayout(mTabLayout);
                        shapeIndicatorView.setupWithViewPager(viewPager);
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }

    private class PagerAdapter extends FragmentStatePagerAdapter {

        public PagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            if (i == 0) {
                LXCNumberTrendFragment lxcTrendFragment = new LXCNumberTrendFragment();
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList("lists", liuhecaiTrendResponseList);
                lxcTrendFragment.setArguments(bundle);
                return lxcTrendFragment;
            } else {
                LXCTrendFragment lxcTrendFragment = new LXCTrendFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("index", i);
                bundle.putParcelableArrayList("lists", liuhecaiTrendResponseList);
                lxcTrendFragment.setArguments(bundle);
                return lxcTrendFragment;
            }
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "号码";
                case 1:
                    return "总和";
                default:
                    return "特码";
            }
        }
    }

    public static void startActivity(Activity activity, String code) {
        Intent intent = new Intent(activity, LhcChartActivity.class);
        intent.putExtra("code", code);
        activity.startActivity(intent);
    }
}
